package DBConnection;

import java.util.List;

import model.entities.WeatherStation;

import org.hibernate.HibernateException;

public class WeatherStationDB extends HibernateUtil implements
		ConnectDB<WeatherStation, Boolean, String> {

	@Override
	public Boolean add(WeatherStation weatherStation) {
		try {
			if (weatherStation != null) {
				this.beginTranSaction();
				this.getCurrentSession().save(weatherStation);
				this.commitTransaction();
			}
		} catch (Exception e) {
				this.rollback();
		}
		return true;
	}

	@Override
	public Boolean delete(String ID) {
		WeatherStation WeatherStation = find(ID);
		try {
			if (WeatherStation != null) {
				this.beginTranSaction();
				this.getCurrentSession().delete(WeatherStation);
				this.commitTransaction();
			}
		} catch (HibernateException e) {
			this.rollback();
			throw e;
		}
		return true;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<WeatherStation> findAll() {
		try {
			this.beginTranSaction();
			return this.getCurrentSession()
					.createCriteria(WeatherStation.class).list();
		} catch (Exception e) {
			this.rollback();
			throw e;
		}
	}

	@Override
	public Boolean update(WeatherStation WeatherStation) {
		try {
			if (WeatherStation != null) {
				this.beginTranSaction();
				this.getCurrentSession().update(WeatherStation);
				this.commitTransaction();
			}
		} catch (Exception e) {
			this.rollback();
			throw e;
		}
		return true;
	}

	@Override
	public WeatherStation find(String ID) {
		try {
			this.beginTranSaction();
			return (WeatherStation) this.getCurrentSession().get(
					WeatherStation.class, ID);
		} catch (Exception e) {
			throw e;
		}
	}

	public static void main(String[] args) {
		List<WeatherStation> ll = new WeatherStationDB().findAll();
		System.out.println(ll.size());
		System.out.println(new WeatherStationDB().find("WS"));
	}

}
